#pragma once

#include <thread>
#include <condition_variable>
#include <chrono>
#include <iostream>

//封装一下condition_variable
class AutoResetEvent {
public:
    AutoResetEvent() : signaled(false) {}

    bool Wait(std::chrono::milliseconds timeout) {
        std::unique_lock<std::mutex> lock(mutex);
        if (!condition.wait_for(lock, timeout, [this] { return signaled; })) {
            // 超时等待
            std::cout << "Wait timeout!" << std::endl;
            return false;
        }
        signaled = false; // 自动重置
        return true;
    }

    void Set() {
        std::lock_guard<std::mutex> lock(mutex);
        signaled = true;

        condition.notify_one();
    }

private:
    std::mutex mutex;
    std::condition_variable condition;
    bool signaled;
};